package projectwork.shchuangge.cn.com.newproject.ui.view;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import projectwork.shchuangge.cn.com.newproject.R;
import projectwork.shchuangge.cn.com.newproject.adapter.ChoiceFragmentListAdpter;
import projectwork.shchuangge.cn.com.newproject.pullToRefreshListView.PullToRefreshBase;
import projectwork.shchuangge.cn.com.newproject.pullToRefreshListView.PullToRefreshListView;
import projectwork.shchuangge.cn.com.newproject.ui.fragment.BeasicFragment;
import projectwork.shchuangge.cn.com.newproject.ui.shownfragment.ShownFragment;
import projectwork.shchuangge.cn.com.newproject.uitls.ToastMake;
import projectwork.shchuangge.cn.com.newproject.uitls.Utils;
import projectwork.shchuangge.cn.com.newproject.weiget.ChoiceHandViewPager;
import projectwork.shchuangge.cn.com.newproject.weiget.RoundCornerImageView;


/**
 * Created by 赵航 on 2017/6/19.
 */

public class ChoiceFragment extends BeasicFragment implements View.OnClickListener {
    private PullToRefreshListView mListView;

    private List<Integer> mList = new ArrayList<>();

    private Integer[] arr = {R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h};
    private Integer[] brr = {R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h};
    private ChoiceHandViewPager       mPager;
    private RelativeLayout            mLayout;
    private LinearLayout              mLinearLayout;
    private View                      mHandLineview;
    private LinearLayout              mButonOne;
    private View                      mButtonTwo;
    private View                      mButtonThree;
    private View                      mButtonFour;
    private View                      mButtonFive;
    private LinearLayout              mShownDesign;
    private LinearLayout.LayoutParams mParams;
    private LinearLayout              mFreePay;
    private LinearLayout.LayoutParams mFerMuying;
    private ChoiceFragmentListAdpter  mAdpter;
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //TODO：网络获取数据进行主线程刷新

            /**下拉模式
             * 先确定模式，然后再做相应的操作
             */

            if (msg.arg1==1){
                if (mList!=null){
                    mList.clear();
                }
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                 //   List<BagFragment> listData = CacheManger.getInstance().getListData(uri+mmList.size(), BagFragment.class);
                }
            }).start();


        }
    };

    private String uri = "";
    private ArrayList<Object> mmList;

    @Override
    protected Object requestNetData() {




        Utils.runOnUIThread(new Runnable() {
            @Override
            public void run() {


                //添加viewpager
                initViewPager();
                //添加小圆点
                initPoints();
                /**
                 * 设置横向的ScrollView进行点击滑动操作
                 */
                initImages();
                /**
                 * 设置抢购
                 */
                initFreeRush();
                //TODO:不重复添加头布局
                //TODO:回复后台的上啦或者下拉刷新字段，来请求数据，并且进行展示
                int count = mListView.getRefreshableView().getHeaderViewsCount();
                if (count == 0) {

                    mListView.getRefreshableView().addHeaderView(mHandLineview);
                }
                mAdpter = new ChoiceFragmentListAdpter(mList);
                //TODO:下拉刷新有可能轮播图数组越界
                if (mAdpter != null) {
                    mPager.stopHandler();

                }

                //设置adapter的点击事假
                mAdpter.setOnImageItemClickeListener(new ChoiceFragmentListAdpter.OnImageItemClicke() {
                    @Override
                    public void onImageItemClicke(String s) {
                        ToastMake.showToast(s);
                        Bundle bundle=new Bundle();
                        String url = "www.baidu.com";
                        bundle.putString("URL",url);
                        //直接一个方法就可以开启
                        startFragment(bundle,ShownFragment.class);
                    }
                });

                mListView.getRefreshableView().setAdapter(mAdpter);



            }
        });


        return " ";
    }


    @Override
    public View creatSuccessView() {

        mList.add(R.drawable.banner_e);
        mList.add(R.drawable.banner_n);
        mList.add(R.drawable.banner_r);
        mList.add(R.drawable.banner_s);

        //初始化布局,并添加
        View viwe = View.inflate(getContext(), R.layout.second_chiocefragment, null);

        mListView = (PullToRefreshListView) viwe.findViewById(R.id.choicefragment_list);
        mHandLineview = View.inflate(getContext(), R.layout.rollviewpager, null);

        //初始化横向的RecollView
        mShownDesign = (LinearLayout) mHandLineview.findViewById(R.id.linar_first_shown_design);

        //第二个横向的ScrollView
        mFreePay = (LinearLayout) mHandLineview.findViewById(R.id.linar_fseconf_shown_freepay);


        /**
         * 添加viewpager的控件
         */
        mLayout = mHandLineview.findViewById(R.id.viewpager_and_point);

        /**
         * 添加小圆点的横条,必须先移除再添加,不然会显示在后面,看不到小圆点
         */
        mLinearLayout = mHandLineview.findViewById(R.id.chioce_linaer_addpoint);
        mLayout.removeView(mLinearLayout);
        /**
         * 添加五个小按钮
         */
        mButonOne = mHandLineview.findViewById(R.id.choice_new_item_clicke);
        mButtonTwo = mHandLineview.findViewById(R.id.choice_cashback_item_clicke);
        mButtonThree = mHandLineview.findViewById(R.id.choice_liveviod_item_clicke);
        mButtonFour = mHandLineview.findViewById(R.id.choice_flashpurchase_item_clicke);
        mButtonFive = mHandLineview.findViewById(R.id.choice_brand_item_clicke);

        //五个子空间的点击事件
        initClicke();
        /**
         * 刷新使用
         */
        //TODO:设置下拉刷新
        //设置可用上拉加载更多可用
        mListView.setPullLoadEnabled(true);


        //设置下拉加载更多
        mListView.setPullRefreshEnabled(true);

        //设置滚动底部加载可用

      //  mListView.setScrollLoadEnabled(false);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            //下拉刷新调用该方法
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                /**
                 * 加载更多，需要给后台传个“时间戳”，载url中需要加入有个more字段
                 * 针对新闻列表进行记录
                 *
                 */

                //重新走一次数据加载。。。。
                refreshListView();

                mHandler.sendEmptyMessage(0);
                //结束下拉刷新
                mListView.onPullDownRefreshComplete();
            }
            //加载更多调用这个方法
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                /**
                 * 下拉刷新，跟距集合的长度动态添加
                 */

                //重新走一次数据加载。。。。
                refreshListView();

                mHandler.sendEmptyMessage(0);
                //结束下拉刷新
                mListView.onPullUpRefreshComplete();
            }
        });
        return viwe;
    }

    /**
     * 这里进行横向的图片滑动点击操作
     */

    private void initImages() {

        for (int i = 0; i < mList.size(); i++) {

            RoundCornerImageView imageView = new RoundCornerImageView(getContext());
            //TODO;绘制圆角矩形有待解决

            mParams = new LinearLayout.LayoutParams(344, 165);
            if (i != 0) {
                mParams.leftMargin = 20;
            }
            mParams.topMargin = 30;
            mParams.bottomMargin = 30;
            imageView.setBackgroundResource(mList.get(i));
            imageView.setLayoutParams(mParams);
            mShownDesign.addView(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //TODO:横向的图片点击事件
                    ToastMake.showToast("这是第" + "张图片");
                }
            });

        }
    }

    /**
     * 这里全部对viewpager进行操作
     */
    private void initViewPager() {
        mPager = new ChoiceHandViewPager(getContext());
//        if (mPager != null) {
//            mPager.stopHandler();
//        }
        mPager.getImages(mList);

        mLayout.addView(mPager);

        mPager.starHandler();
        mPager.setOnItemCLickListener(new ChoiceHandViewPager.OnItemCLickListener() {
            @Override
            public void OnItemCLick(int position) {
                ToastMake.showToast(position + "");
            }
        });

    }

    private void initPoints() {

        mPager.setLinearLayout(mLinearLayout);

        mLayout.addView(mLinearLayout);
    }

    /**
     * 五个控件的点击事件
     */
    private void initClicke() {
        mButonOne.setOnClickListener(this);
        mButtonTwo.setOnClickListener(this);
        mButtonThree.setOnClickListener(this);
        mButtonFour.setOnClickListener(this);
        mButtonFive.setOnClickListener(this);

    }

    /**
     * 五个控件的点击事件
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //上新
            case R.id.choice_new_item_clicke:
                ToastMake.showToast("上新");
                break;
            //返现
            case R.id.choice_cashback_item_clicke:
                ToastMake.showToast("返现");
                break;
            //直播购物
            case R.id.choice_liveviod_item_clicke:
                ToastMake.showToast("直播购物");
                break;
            //闪购
            case R.id.choice_flashpurchase_item_clicke:
                ToastMake.showToast("闪购");
                break;
            //品牌
            case R.id.choice_brand_item_clicke:
                ToastMake.showToast("品牌");
                break;

        }


    }

    /**
     * 免费抢购的第二个侧滑图
     */

    private void initFreeRush() {
        for (int i = 0; i < mList.size(); i++) {
            //先随便画个矩形
            LinearLayout linearLayout = new LinearLayout(getContext());
            mFerMuying = new LinearLayout.LayoutParams(184, 300);
            if (i == 0) {
                mFerMuying.leftMargin = 30;
            } else {
                mFerMuying.leftMargin = 20;
            }
            mFerMuying.topMargin = 26;
            mFerMuying.bottomMargin = 30;
            mFerMuying.gravity = Gravity.CENTER_VERTICAL;

            //这里设置好了父布局
            linearLayout.setLayoutParams(mFerMuying);

            //这里初始化控件ImageView
            ImageView imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            LinearLayout.LayoutParams imagwTwo = new LinearLayout.LayoutParams(184, 228);
            imageView.setLayoutParams(imagwTwo);
            imageView.setBackgroundResource(mList.get(i));



            //这里初始化控件TextView
            TextView textView = new TextView(getContext());
            textView.setTextSize(20);
            textView.setPadding(0, 18, 0, 0);
            textView.setTextColor(Color.parseColor("#333333"));
            textView.setText("¥ 1000");

            //设置布局显示方式
            linearLayout.setOrientation(LinearLayout.VERTICAL);

            //添加布局
            linearLayout.addView(imageView);
            linearLayout.addView(textView);
            mFreePay.addView(linearLayout);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //TODO:横向的图片点击事件
                    ToastMake.showToast("这是第" + "张图片");
                }
            });
        }
    }
}
